﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Template
{
    public class StringHandler
    {
        public const string _replaceChar = "@@";
        public const string _splitChar = "#@#";
        public static string RemoveBlank(string orginStr)
        {
            return orginStr.Replace(" ", "");
        }

        public static string RemoveString(string orginStr, string removeStr)
        {
            return orginStr.Replace(removeStr, "");
        }

        public static string RemoveString(string orginStr, string[] removeStr)
        {
            string rst = orginStr;
            foreach(string str in removeStr)
                rst = rst.Replace(str, "");
            return rst;
        }

        public static string RemoveBefore(string orginStr, string befStr)
        {
            string rst = orginStr;
            int index = rst.IndexOf(befStr);
            if(index>0)
            {
                rst = rst.Substring(index+1);
            }
            return rst;
        }

        public static string RemoveBack(string orginStr, string backStr)
        {
            string rst = orginStr;
            int index = rst.IndexOf(backStr);
            if (index > 0)
            {
                rst = rst.Substring(0, index);
            }
            return rst;
        }

        public static string RemoveMiddle(string orginStr, string midStr1, string midStr2)
        {
            string rst = orginStr;
            int index1 = rst.IndexOf(midStr1), index2 = rst.IndexOf(midStr2);
            if (index1 > 0 && index2 > 0 && index2>index1)
            {
                rst = rst.Substring(index1, index2 - index1 - 1);
            }
            return rst;
        }

        public static string AddBefore(string orginStr, string beforeStr)
        {
            return beforeStr + orginStr;
        }

        public static List<string> ApplyRule(string orginStr, string rule)
        {
            List<string> rstList=new List<string>();
            if (string.IsNullOrEmpty(rule))
            { rstList.Add(orginStr); return rstList;}
            string[] arr = rule.Split('-');
            if (arr.Length != 3)
            { rstList.Add(orginStr); }
            else
            {
                switch (arr[0])
                {
                    case "int":
                        int startIdx = 0, endIdx = 100;
                        int.TryParse(arr[1], out startIdx);
                        if (arr[2] != "n")
                            int.TryParse(arr[2], out endIdx);
                        for (int i = startIdx; i <= endIdx; i++)
                            rstList.Add(orginStr.Replace(_replaceChar, i.ToString()));
                        break;
                    case "letter":
                        string startChar = arr[1], endChar = arr[2];
                        for (int i = (int)arr[1].ToCharArray()[0]; i <= (int)arr[2].ToCharArray()[0]; i++)
                            rstList.Add(orginStr.Replace(_replaceChar, ((char)i).ToString()));
                        break;
                }
            }
            return rstList;
        }

        public string Add20ForTime(string originStr)
        {
            return "20" + originStr;
        }

        public DateTime ConvertToDateTime(string originStr)
        {
            DateTime time = DateTime.MinValue;
            try
            {
                time = Convert.ToDateTime(originStr);
            }
            catch { }
            return time;
        }
    }
}
